package com.hainatong.shiphelper.login.view;


import com.base.common.model.http.err.ApiException;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.OnClickCheckedUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseActivity;
import com.base.common.viewmodel.BaseViewObserver;
import com.blankj.utilcode.util.*;

import com.hainatong.shiphelper.*;
import com.hainatong.shiphelper.api.PublicValues;
import com.hainatong.shiphelper.databinding.ActivityLoginBinding;
import com.hainatong.shiphelper.login.model.UserInfo;
import com.hainatong.shiphelper.login.vm.LoginVM;
import com.hainatong.shiphelper.mainhome.MainHomeActivity;
import com.hainatong.shiphelper.utils.ClickUtil;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;


public class LoginActivity extends BaseActivity<ActivityLoginBinding, LoginVM> {

    public static String Token_Unauthorized = "TokenUnauthorized";

    private boolean isOpenPwd = false;

    public static void newLoginStart(Context context)
    {
        Intent start = new Intent(context, LoginActivity.class);
        start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        context.startActivity(start);
        ((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }


    public static void start(Context context) {
        Intent start = new Intent(context, LoginActivity.class);
        context.startActivity(start);
        ((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
    }



    @Override
    protected void initView() {

        binding.btnLogin.setOnClickListener(new OnClickCheckedUtil() {

            @Override
            public void onClicked(View view) {

                String userName = binding.etUsername.getText().toString().trim();
                String pwd = binding.etPassword.getText().toString().trim();

                if (!StringUtils.isEmpty(userName)) {
//                     if(!Validator.isMobile( viewModel.username.get() ))
                    if (false) {
//                        ToastUtils.showShort(getString(R.string.error_phone_tips));
                        UIUtils.showToastSafesClose(getString(R.string.error_phone_tips));
                        return;
                    }
                } else {
//                    ToastUtils.showShort(getString(R.string.input_phone));
                    UIUtils.showToastSafesClose(getString(R.string.input_phone));
                    return;
                }


                if (!StringUtils.isEmpty(pwd)) {
                    // if( !Validator.isPassword( viewModel.pwd.get() ) )
                    if (false) {
//                        ToastUtils.showShort(getString(R.string.error_pwd_tips));
                        UIUtils.showToastSafesClose(getString(R.string.error_pwd_tips));
                        return;
                    }
                } else {
//                    ToastUtils.showShort(getString(R.string.input_pwd));
                    UIUtils.showToastSafesClose(getString(R.string.input_pwd));
                    return;
                }


                viewModel.login(userName, pwd).observe(getActivity(), new BaseViewObserver<LiveDataWrapper<UserInfo>>(getActivity()) {
                    @Override
                    protected void onSuccess(LiveDataWrapper<UserInfo> data) {

                        PublicValues.setUserInfo(data.data.getData());
                        // 全局  用户信息
                        GlobalConfig.getInstance().setUserInfo( data.data.getData() );
                        UIUtils.showToastSafes(R.string.login_success_str);
                        MainHomeActivity.start(LoginActivity.this);
                        finish();
                    }

                    @Override
                    public boolean isEmptyData(LiveDataWrapper<UserInfo> data) {
                        return UIUtils.isEmpty(data.data.getData());
                    }

                    @Override
                    protected void onAppError(ApiException error) {
                        super.onAppError(error);
                    }
                });


                // startActivity( new Intent( this,ListActivity.class ) );
            }
        });


        binding.ivShowPwd.setOnClickListener(view -> {

            if (ClickUtil.isFastClick(500, view)) {
                return;
            }
            isOpenPwd = !isOpenPwd;
            if (isOpenPwd) {
                // 明文
                binding.etPassword.setTransformationMethod(
                        HideReturnsTransformationMethod.getInstance());
                binding.ivShowPwd.setImageResource(R.mipmap.ic_eye_open);
            } else {
                // 显示密文
                binding.etPassword.setTransformationMethod(
                        PasswordTransformationMethod.getInstance());
                binding.ivShowPwd.setImageResource(R.mipmap.ic_eye_close);
            }
        });

        binding.forgetPwd.setOnClickListener(v -> {
            ToastUtils.showShort(getString(R.string.call_manager));
            // 测试用
            //WebViewActivity.start(this, "http://192.168.0.2/pano/index.html");
            //startActivity( new Intent( this, SecondActivity.class) );
        });
    }

    @Override
    protected void initData() {

    }


}
